package com.max.back.demo.api;

import cn.hutool.core.bean.BeanUtil;
import com.max.back.common.http.ResponseResult;
import com.max.back.common.sercurity.anno.AuthIgrone;
import com.max.back.demo.entity.Bcodename;
import com.max.back.demo.form.CodenameForm;
import com.max.back.demo.form.DrugTypeDeleteForm;
import com.max.back.demo.service.BcodenameSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author max
 * @since 2021-07-29
 */
//@Authent
@CrossOrigin
@RestController
@RequestMapping("/api/bcodename")
public class BcodenameCtl {
    @Autowired
    private BcodenameSer ser;

    @AuthIgrone
    @PostMapping("/getcodelist")
    public ResponseResult<List<Bcodename>> getCodeList(@RequestBody @Validated({CodenameForm.Getlist.class}) CodenameForm form) {
        List<Bcodename> list = ser.queryByCodeType(form.getCodeid());
        return ResponseResult.getSuccessResult(list);
    }

    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Validated({CodenameForm.Base.class}) CodenameForm form) {
        Bcodename bcodename = BeanUtil.toBean(form, Bcodename.class);
        boolean res = ser.checksave(bcodename);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody @Validated({CodenameForm.Delete.class}) CodenameForm form) {
        boolean res = ser.removeById(form.getId());
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/countdelete")
    public ResponseResult<Boolean> countdelete(@RequestBody @Valid DrugTypeDeleteForm form) {
        boolean res = false;
        for (String id : form.getIds()) {
            res = ser.removeById(id);
        }
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody @Validated({CodenameForm.Update.class}) CodenameForm form) {
        Bcodename bcodename = BeanUtil.toBean(form, Bcodename.class);
        boolean res = ser.updateById(bcodename);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/find")
    public ResponseResult<List<Bcodename>> find(@RequestBody @Validated({CodenameForm.Find.class}) CodenameForm form) {
        List<Bcodename> list = ser.findcode(form.getName());
        return ResponseResult.getSuccessResult(list);
    }
}

